﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RobotGameData;

namespace RobotGameData
{
    public class Camera
    {
        #region Properties

        public Vector2 MapSize { get; set; }

        public Vector2 TileSize { get; set; }

        public Vector2 ViewSize { get; set; }

        public float Speed { get; set; }

        public Character Focus { get; set; }

        public Vector2 ViewCenter { get; set; }

        public Rectangle VisibleArea
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y, (int)(ViewSize.X + Position.X), (int)(ViewSize.Y + Position.Y));
            }
        }

        public Matrix Matrix
        {
            get
            {
                return Matrix.CreateTranslation(-(int)Position.X, -(int)Position.Y, 0);
            }
        }

        public Vector2 Position { get; set; }

        #endregion

        #region Constructor

        public Camera()
        {
            MapSize = Vector2.One;
            TileSize = Vector2.One;
            ViewSize = Vector2.One;
            Speed = 1f;
        }

        public Camera(Vector2 mapSize, Vector2 tileSize, Vector2 viewSize)
        {
            MapSize = mapSize;
            TileSize = tileSize;
            ViewSize = viewSize;
            Speed = TileSize.X * 0.625f;
            ViewCenter = new Vector2(viewSize.X / 2, viewSize.Y / 2);
            Position = Vector2.Zero;
        }

        #endregion

        #region Methods

        public void Update(InputState input)
        {
            Vector2 cameraMovement = Vector2.Zero;

            if (input.IsNewKeyPress(Keys.Left))
            {
                cameraMovement.X = -1;
            }
            else if (input.IsNewKeyPress(Keys.Right))
            {
                cameraMovement.X = 1;
            }
            if (input.IsNewKeyPress(Keys.Up))
            {
                cameraMovement.Y = -1;
            }
            else if (input.IsNewKeyPress(Keys.Down))
            {
                cameraMovement.Y = 1;
            }

            if (cameraMovement != Vector2.Zero)
            {
                cameraMovement.Normalize();
            }


            if (Focus == null)
            {
                Position += Speed * cameraMovement;
            }
            else
            {
                CenterOnFocus();
            }
            Vector2 cameraMax = new Vector2(MapSize.X * TileSize.X - ViewSize.X, MapSize.Y * TileSize.Y - ViewSize.Y);
            Position = Vector2.Clamp(Position, Vector2.Zero, cameraMax);
        }

        public void CenterOnFocus()
        {
            Position = Focus.Position - ViewCenter;
        }

        public void FocusOn(Character character)
        {
            Focus = character;
            CenterOnFocus();
        }

        #endregion
    }
}
